package com.yoomiito.app.adapter.my;

import android.net.Uri;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.model.my.MyOrderBean;
import com.yoomiito.app.ui.fragment.my.MyMonthOrderFragment;
import com.yoomiito.app.ui.fragment.my.MyOrderFragment;
import com.yoomiito.app.ui.fragment.my.MyVipFragment;
import com.yoomiito.app.utils.DataUtils;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.widget.CircleImageView;

import java.util.List;

public class MyOrderAdapter extends BaseQuickAdapter<MyOrderBean, BaseViewHolder> {
    public final static int ORDER_TYPE_NOMAL = 1;
    public final static int ORDER_TYPE_VIP= -1;

    private int type = ORDER_TYPE_NOMAL;
    private String pageType;

    public MyOrderAdapter(@Nullable List<MyOrderBean> data, int type, String pageType) {
        super(R.layout.item_my_order, data);
        this.type = type;
        this.pageType = pageType;
    }

    @Override
    protected void convert(final BaseViewHolder helper, MyOrderBean item) {
        ((TextView)helper.getView(R.id.tv_name)).setText(item.getNickname());
        if (MyMonthOrderFragment.MyMonthOrderFm.equals(pageType)){
            ((TextView)helper.getView(R.id.tv_order_num)).setText(ResUtil.getString(R.string.my_order_num) + item.getOrder_id());
        }else if (MyOrderFragment.MyOrderFm.equals(pageType)){//今日订单
            if (Constant.NAMAL_TYPE_CREDITCARD==type){
                ((TextView)helper.getView(R.id.tv_order_num)).setText(ResUtil.getString(R.string.my_order_num) + item.getOrder_id());
            }else if (TextUtils.isEmpty(item.getOrder_source())){
                ((TextView)helper.getView(R.id.tv_order_num)).setText(ResUtil.getString(R.string.my_order_num) + item.getSn());
            }else {
                ((TextView)helper.getView(R.id.tv_order_num)).setText(ResUtil.getString(R.string.my_order_num) + item.getOrder_id());
            }
            if (Constant.NAMAL_TYPE_TAOBAO != type){
                TextView goodsName = helper.getView(R.id.item_order_goods_name);
                goodsName.setText(item.getProduct_name());
                goodsName.setVisibility(View.VISIBLE);
            }
        }else {
            ((TextView)helper.getView(R.id.tv_order_num)).setText(ResUtil.getString(R.string.my_order_num) + item.getSn());
        }

        if (MyVipFragment.MyVipFm.equals(pageType)){
            TextView goodsName = helper.getView(R.id.item_order_goods_name);
            goodsName.setText(item.getProduct_name());
            goodsName.setVisibility(View.VISIBLE);
        }

        ((TextView)helper.getView(R.id.tv_money)).setText(""+ SpannableUtils.handlePrice(item.getUser_income()));
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.goods_default);
        if (!TextUtils.isEmpty(item.getHeadimgurl())){
            Glide.with(App.getApplication()).load(Uri.parse(item.getHeadimgurl())).apply(requestOptions).into((CircleImageView)helper.getView(R.id.iv_photo));
        }
        ImageView flagIv = helper.getView(R.id.iv_st);
        if (Constant.NAMAL_TYPE_TAOBAO == type||Constant.NAMAL_TYPE_JINGDONG==type){
            int resID = DataUtils.INSTANCE.ivType(item.getOrder_source());
            flagIv.setVisibility(resID==-1?View.GONE:View.VISIBLE);
            flagIv.setImageResource(resID);
        }else {
            if (TextUtils.isEmpty(item.getImage_url())){
                flagIv.setVisibility(View.GONE);
            }else {
                flagIv.setVisibility(View.VISIBLE);
                ImageLoader.getInstance().into(flagIv.getContext(),item.getImage_url(),flagIv);
            }
//            if (item.isIs_agent()){
//                flagIv.setVisibility(View.VISIBLE);
//                flagIv.setImageResource(R.drawable.icon_huangguan);
//            }else {
//                flagIv.setVisibility(View.GONE);
//            }
        }

        //优品爆品 MyVipFm  2 取消结算  1  已结算  0 待结算  同今日订单
        // 我的订单同订单列表
        // 本月结算收益 * status  1; //待结算  2; //流转到下月结算 3;  //失效订单
        View failView = helper.getView(R.id.item_my_order_fail);
        if (MyMonthOrderFragment.MyMonthOrderFm.equals(pageType)){//月度
            switch (item.getStatus()){
                case 1:
                    helper.getView(R.id.iv_status).setVisibility(View.GONE);
                    failView.setVisibility(View.GONE);
                    break;
                case 2:
                    helper.getView(R.id.iv_status).setVisibility(View.VISIBLE);
                    ((ImageView)helper.getView(R.id.iv_status)).setImageResource(R.mipmap.lastmonth_settled);
                    failView.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    failView.setVisibility(View.VISIBLE);
                    helper.getView(R.id.iv_status).setVisibility(View.VISIBLE);
                    ((ImageView)helper.getView(R.id.iv_status)).setImageResource(R.mipmap.failure);
                    break;
            }
        }else if (MyOrderFragment.MyOrderFm.equals(pageType)){//今日订单
//            if (Constant.NAMAL_TYPE_GIVEMORE == type){//送多多  只显示已结算状态 不显示待结算状态
//                failView.setVisibility(View.VISIBLE);
//                ((ImageView)helper.getView(R.id.iv_status)).setImageResource(R.mipmap.already_settled);
//            }else {
            helper.addOnClickListener(R.id.tipIv);
            helper.setGone(R.id.tipIv,Constant.NAMAL_TYPE_FREEGIFT == type);
                switch (item.getStatus()){
                    case Constant.ORDER_STATUS_WAIT_PAY:
                        failView.setVisibility(View.GONE);
                        helper.getView(R.id.iv_status).setVisibility(View.GONE);
                        break;
                    case Constant.ORDER_STATUS_PAYED:
                        if ((Constant.NAMAL_TYPE_FREEGIFT != type&&Constant.NAMAL_TYPE_GIVEMORE != type)){
                            failView.setVisibility(View.VISIBLE);
                        }
                        helper.getView(R.id.iv_status).setVisibility(View.VISIBLE);
                        ((ImageView)helper.getView(R.id.iv_status)).setImageResource(R.mipmap.already_settled);
                        break;
                    case Constant.ORDER_STATUS_CANCEL_PAY:
                        failView.setVisibility(View.VISIBLE);
                        helper.getView(R.id.iv_status).setVisibility(View.VISIBLE);
                        ((ImageView)helper.getView(R.id.iv_status)).setImageResource(R.mipmap.failure);
                        break;
                }
//            }
        }else {
            switch (item.getStatus()){
                case Constant.ORDER_STATUS_WAIT_PAY:
                    helper.getView(R.id.iv_status).setVisibility(View.GONE);
                    failView.setVisibility(View.GONE);
                    break;
                case Constant.ORDER_STATUS_PAYED:
                    failView.setVisibility(View.VISIBLE);
                    helper.getView(R.id.iv_status).setVisibility(View.VISIBLE);
                    ((ImageView)helper.getView(R.id.iv_status)).setImageResource(R.mipmap.already_settled);
                    break;
                case Constant.ORDER_STATUS_CANCEL_PAY:
                    failView.setVisibility(View.VISIBLE);
                    helper.getView(R.id.iv_status).setVisibility(View.VISIBLE);
                    ((ImageView)helper.getView(R.id.iv_status)).setImageResource(R.mipmap.failure);
                    break;
            }
        }


//        switch (type){
//            case ORDER_TYPE_NOMAL:
//                helper.getView(R.id.tv_date).setVisibility(View.GONE);
//                break;
//            case ORDER_TYPE_VIP:
//                helper.getView(R.id.tv_date).setVisibility(View.VISIBLE);
                ((TextView)helper.getView(R.id.tv_date)).setText(item.getCtime());
//                break;
//        }

    }
}
